layui.use(['form', 'table'], function () {
    var $ = layui.$;
    var form = layui.form;
    var table = layui.table;

    /**
     * 套餐资费--资费信息
     */
    var Charges = {
        tableId: "chargesTable",    //表格id
        condition: {
            accountId: "",
            offeringId:""
        }
    };

    /**
     * 查询所有的账号信息，填充select标签
     */
    function searchAccount(accountId) {
        $('#accountId').html('');
        $("#accountId").append('<option value="" >请选择</option>');
        $.ajax({
            type: 'get',
            url: Feng.ctxPath + "/account/allSelectAccounts",
            async: false,
            success: function (data) {
                for (var i = 0; i < data.data.length; i++) {
                    var name = data.data[i].accountName;
                    var code = data.data[i].accountId;
                    if (accountId == code) {
                        $("#accountId").append('<option value="' + code + '" selected>' + name + '</option>');
                    } else {
                        $("#accountId").append('<option value="' + code + '">' + name + '</option>');
                    }
                }
                form.render('select');
            }
        });
    }
    searchAccount();

    /**
     * 初始化表格的列
     */
    Charges.initColumn = function () {
        return [[
            {field: 'offeringId', align: "center", title: '资费ID'},
            {field: 'offeringName', align: "center", title: '资费名称'},
            {field: 'effectiveDate', align: "center", title: '生效时间'},
            {field: "expiriedDate", align: "center", title: '失效时间'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Charges.searchCharges = function () {
        var accountId = $("#accountId").val();
        var queryData = {};
        queryData['accountId'] = accountId;
        queryData['offeringId'] = $("#offeringId").val();
        table.reload(Charges.tableId, {
            where: queryData
        });
        searchAccount(accountId);//每次点查询就重置;
    };
    $("#btnSearch").click(function () {
        Charges.searchCharges();
    });

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Charges.tableId,
        url: Feng.ctxPath + '/charges/list',
        page: true,
        limits: [10,20,30,40,50],
        height: "full-98",
        cellMinWidth: 100,
        cols: Charges.initColumn()
    });
});